//
// Created by zhengbo on 23-4-18.
//

#ifndef VISUALIZATION_TOOL_RENDERER_H
#define VISUALIZATION_TOOL_RENDERER_H

#include <memory>
#include "../scene/SceneManager.h"

class Renderer {
public:
    Renderer(std::shared_ptr<SceneManager>& sceneManager);
    void Render();
    void SetCameraPose(const Vector3f& position, const Quaternionf& orientation);
    void SetOutputBuffer(unsigned char* buffer, int width, int height);
private:
    std::shared_ptr<SceneManager> sceneManager;
    Camera mCamera;
    unsigned char* mOutputBuffer;
    int mOutputWidth;
    int mOutputHeight;
};

Renderer::Renderer(std::shared_ptr<SceneManager>& sceneManager)
        : sceneManager(sceneManager)

{
    mCamera.SetFieldOfView(60.0f);
    mCamera.SetAspectRatio(16.0f/9.0f);
    mCamera.SetNearPlane(0.1f);
    mCamera.SetFarPlane(1000.0f);
}

void Renderer::Render() {
    memset(mOutputBuffer, 0, mOutputWidth * mOutputHeight * 4);
    auto objects = sceneManager->GetObjects();
    for (const auto object : objects) {
        object->Update();
    }
    for (const auto object : objects) {
        object->Render();
    }
}

void Renderer::SetCameraPose(const Vector3f& position, const Quaternionf& orientation) {
    mCamera.SetPosition(position);
    mCamera.SetOrientation(orientation);
}

void Renderer::SetOutputBuffer(unsigned char* buffer, int width, int height) {
    mOutputBuffer = buffer;
    mOutputWidth = width;
    mOutputHeight = height;
}

#endif //VISUALIZATION_TOOL_RENDERER_H
